package view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.concerneyes.R;


public class PointerView extends LinearLayout {

    private ImageView[] dotview;
    private int mCount = 0;
    private int position = 0;
    protected int normal = R.drawable.dot_white;
    protected int focus = R.drawable.dot_blue;
    private LayoutParams params = null;
    private int padding = 12;
    private int margin = 12;
    private int size = 25;

    public PointerView(Context context) {
        super(context);
        init();
    }

    public PointerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        setVisibility(View.INVISIBLE);
    }

    public void setDotRes(int normal, int focus) {
        this.focus = focus;
        this.normal = normal;
    }

    public void show(int cout) {
        if (cout > 0) {
            this.mCount = cout;
            buildView();
        }
    }

    public void setMargin(int margin) {
        this.margin = margin;
    }

    public void setPadding(int padding) {
        this.padding = padding;
    }

    public void setSize(int size) {
        this.size = size;
    }
    private void buildView() {
        removeAllViews();

        int pad = this.padding;
        setPadding(pad, pad, pad, pad);
        dotview = new ImageView[this.mCount];
        int size = this.size;

        int mar = margin;
        params = new LayoutParams(size, size);
        params.setMargins(mar, 0, mar, 0);
        
        for (int i = 0; i < mCount; i++) {
            dotview[i] = new ImageView(getContext());
            dotview[i].setPadding(pad, pad, pad, pad);
            if (i != 0) {
                dotview[i].setBackgroundResource(normal);
            } else {
                dotview[i].setBackgroundResource(focus);
            }
            addView(dotview[i], params);
        }
        setVisibility(View.VISIBLE);
    }

    public void next() {
        showWhitch(position + 1);
    }

    public void pre() {
        showWhitch(position - 1);
    }

    public void showWhitch(int pos) {
        if (pos < mCount && pos >= 0) {
            this.position = pos;
            refresh();
        }
    }

    public int getCount() {
        return mCount;
    }

    public int getposition() {
        return position;
    }

    private void refresh() {
        for (int i = 0; i < this.mCount; i++) {
            if (i != position) {
                dotview[i].setBackgroundResource(normal);
            } else {
                dotview[i].setBackgroundResource(focus);
            }
        }
    }
}
